package com.lu.servlet;

import com.lu.pojo.Goods;
import com.lu.pojo.Page;
import com.lu.pojo.Tradelog;
import com.lu.service.impl.BuyServiceImpl;
import com.lu.service.impl.GoodsServiceImpl;
import com.lu.service.impl.TradelogServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet(name = "TradelogServlet",urlPatterns = {"/Tradelog"})
public class TradelogServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter out = resp.getWriter();

        String opr = req.getParameter("opr");
        if("getAll".equals(opr)){
            int total = new TradelogServiceImpl().getCountTradelog();
            int count = 5;
            Page tradePage = new Page(total,count);
            if(req.getParameter("currentPage")!=null){
                int c = Integer.parseInt(req.getParameter("currentPage"));
                tradePage.setCurrentPage(c);
            }
            List<Tradelog> someTradelogList = new TradelogServiceImpl().getSomeTradelogByPage(tradePage);
            req.getSession().setAttribute("tradePage",tradePage);
            req.getSession().setAttribute("someTradelogList",someTradelogList);
            req.getRequestDispatcher("tradelogList.jsp").forward(req,resp);
        } else if("add".equals(opr)){
            int gid = Integer.parseInt(req.getParameter("gid"));
            String trname = req.getParameter("gname");
            Double price = Double.parseDouble(req.getParameter("price"));
            int seller = Integer.parseInt(req.getParameter("seller"));
            int buyer = Integer.parseInt(req.getParameter("buyer"));
            String address = req.getParameter("address");
            String created_By = req.getParameter("created_By");
            String updated_By = req.getParameter("updated_By");
            if (seller == buyer) {
                out.print("<script>alert('不能购买自己所出售的商品');location.href='Goods?opr=getAll'</script>");
            } else {
                double sellerMoney = new BuyServiceImpl().getBuyerMoney(buyer);
                if (sellerMoney > price) {
                    Tradelog tradelog = new Tradelog(gid, trname, price, seller, buyer, created_By, address);
                    int i = new TradelogServiceImpl().addOneTrade(tradelog);
                    if (i > 0) {
                        String sell = "是";
                        int i1 = new GoodsServiceImpl().updateSell(new Goods(gid, sell, updated_By));
                        if (i1 > 0) {
                            int i2 = new BuyServiceImpl().updateSeller(seller, price);
                            int i3 = new BuyServiceImpl().updateBuyer(buyer, price);
                            if (i2 > 0 && i3 > 0) {
                                out.print("<script>alert('购买成功!');location.href = 'admin_1.jsp';</script>");
                            } else {
                                out.print("<script>alert('购买失败!');history.back();</script>");
                            }
                        } else {
                            out.print("<script>alert('购买失败!');history.back();</script>");
                        }
                    } else {
                        out.print("<script>alert('购买失败!');history.back();</script>");
                    }
                } else {
                    out.print("<script>alert('您的余额不足,购买失败!');history.back();</script>");
                }
            }
        }

        out.flush();
        out.close();
    }
}
